package com.youlu.campus.website.service.impl;

import com.youlu.campus.website.common.enums.PlatformType;
import com.youlu.campus.website.entity.CompanyDynamicDetail;
import com.youlu.campus.website.entity.CompanyDynamicType;
import com.youlu.campus.website.entity.CompanyDynamicTypeMonthVo;
import com.youlu.campus.website.entity.CompanyDynamicTypeVo;
import com.youlu.campus.website.service.CompanyDynamicDetailService;
import com.youlu.campus.website.service.CompanyDynamicService;
import com.youlu.campus.website.service.CompanyDynamicTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 公司动态 服务实现类
 */
@Service
public class CompanyDynamicServiceImpl implements CompanyDynamicService {

    @Autowired
    private CompanyDynamicDetailService companyDynamicDetailService;

    @Autowired
    private CompanyDynamicTypeService companyDynamicTypeService;


    @Override
    public CompanyDynamicTypeMonthVo getCurrentMonthDynamic(PlatformType platform) {
        CompanyDynamicTypeMonthVo result = new CompanyDynamicTypeMonthVo();

        CompanyDynamicDetail newDynamicDetail = companyDynamicDetailService.findFirstByPlatformTypeOrderByPublishTimeDesc(platform);
        if (Objects.isNull(newDynamicDetail)) {
            LocalDateTime now = LocalDateTime.now();
            result.setCurrentMonth(now.getMonthValue() + "");
            result.setCurrentYear(now.getYear() + "");
            return result;
        }
        LocalDateTime publishTime = newDynamicDetail.getPublishTime();

        result.setCurrentMonth(publishTime.getMonthValue() + "");
        result.setCurrentYear(publishTime.getYear() + "");

        LocalDateTime startTime = publishTime
                .withDayOfMonth(1)  // 调整为当月1号（消除当前日的影响）
                .withHour(0)        // 时：调整为0点
                .withMinute(0)      // 分：调整为0分
                .withSecond(0)      // 秒：调整为0秒
                .withNano(0);       // 纳秒：调整为0（避免毫秒/纳秒残留）

        LocalDateTime endTime = startTime.plusMonths(1);   // 先加1个月（定位到下个月）


        //获取当前月的动态信息
        List<CompanyDynamicDetail> dynamicDetails = companyDynamicDetailService.findByPlatformAndPublishTimeBetween(platform,startTime, endTime);
        //保证顺序 for循序 分组
        Map<String, List<CompanyDynamicDetail>> map = new LinkedHashMap<>();
        for (CompanyDynamicDetail dynamicDetail : dynamicDetails) {
            String type = dynamicDetail.getType();
            if (!map.containsKey(type)) {
                map.put(type, new ArrayList<>());
            }
            map.get(type).add(dynamicDetail);
        }

        List<CompanyDynamicTypeVo> objects = new ArrayList<>(map.size());
        result.setTypeList(objects);


        List<CompanyDynamicType> typeList = companyDynamicTypeService.findByTypeIn(new ArrayList<>(map.keySet()));
        Map<String, CompanyDynamicType> typeMap = typeList.stream().collect(Collectors.toMap(CompanyDynamicType::getType, Function.identity(), (o1, o2) -> o2));


        for (Map.Entry<String, List<CompanyDynamicDetail>> entry : map.entrySet()) {
            String type = entry.getKey();

            CompanyDynamicType companyDynamicType = typeMap.get(type);
            CompanyDynamicTypeVo companyDynamicTypeVo = new CompanyDynamicTypeVo();
            companyDynamicTypeVo.setId(companyDynamicType.getId());
            companyDynamicTypeVo.setType(type);
            companyDynamicTypeVo.setDetails(entry.getValue());
            companyDynamicTypeVo.setName(companyDynamicType.getName());
            companyDynamicTypeVo.setDescription(companyDynamicType.getDescription());
            objects.add(companyDynamicTypeVo);
        }
        return result;
    }
}